package com.ayh.campusfun.controller.api.v1;

import com.ayh.campusfun.common.CommonResult;
import com.ayh.campusfun.common.PageResult;
import com.ayh.campusfun.common.core.jwt.JWTUtils;
import com.ayh.campusfun.common.exception.http.ForbiddenException;
import com.ayh.campusfun.entity.Attention;
import com.ayh.campusfun.entity.FunShare;
import com.ayh.campusfun.entity.LikeFunTopic;
import com.ayh.campusfun.entity.Users;
import com.ayh.campusfun.service.*;
import com.ayh.campusfun.vo.ShareVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 趣事表(FunShare)表控制层
 *
 * @author ayh
 * @since 2021-03-20 20:42:37
 */
@RestController
@RequestMapping("funShare")
public class FunShareController {
    /**
     * 服务对象
     */
    @Autowired
    private FunShareService funShareService;

    @Autowired
    private TopicShareService topicShareService;

    @Autowired
    private LikeFunTopicService likeFunTopicService;

    @Autowired
    private AttentionService attentionService;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Resource
    private JWTUtils jwtUtils;

    /**
     * 获取趣事列表
     * @param limit 分页条数
     * @param page 页数
     * @return
     */
    @PostMapping(value = "/findFunList")
    public CommonResult getFunList(
            HttpServletRequest request,
            @RequestParam(defaultValue = "15") Integer limit,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "1") Integer funClassifyId){
        PageResult<FunShare> pageResult = funShareService.getFunList(request,limit,page,funClassifyId);
       return CommonResult.success(pageResult);
    }

    /**
     * 点赞，取消点赞
     * @param request 请求
     * @param funOrTopic 趣事还是话题点赞
     * @param id 帖子ID
     * @param clickType 点赞还是取消点赞
     * @return
     */
    @PostMapping(value = "/shareClick")
    public CommonResult shareClick(HttpServletRequest request, Integer funOrTopic,Long id,Integer clickType){
        String uid = jwtUtils.getUidFromRequest(request);
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("clickType",clickType);
        int funType = 1;
        if(funOrTopic==1){
            funShareService.shareClick(map);
        }else if(funOrTopic==2){
            topicShareService.shareClick(map);
            funType = 2;
        }
        if(clickType==1){
            LikeFunTopic likeFunTopic = new LikeFunTopic(null,uid,1,id,funType);
            likeFunTopicService.insert(likeFunTopic);
        }else if(clickType==2){
            map.clear();
            map.put("funId",id);
            map.put("funType",funType);
            likeFunTopicService.deleteByParam(map);
        }
        return CommonResult.successNoData();
    }

    /**
     * 获取趣事的详细信息
     * @param request 请求
     * @param funId 趣事ID
     * @return
     */
    @PostMapping(value = "/findFunShareDetail",produces = "application/json")
    public CommonResult findFunShareDetail(HttpServletRequest request,Long funId){
        FunShare funShare = funShareService.findById(funId);
        String uid = jwtUtils.getUidFromRequest(request);
        Map<String,Object> queryMap = new HashMap<>();
        boolean isClick = false;
        boolean isAttention = false;

        queryMap.put("uid",uid);
        queryMap.put("funId",funId);
        queryMap.put("funType",1);
        LikeFunTopic likeFunTopic = likeFunTopicService.findByParam(queryMap);

        queryMap.clear();
        queryMap.put("uid",uid);
        queryMap.put("attentionId",funShare.getUid());
        Attention attention = attentionService.findByParam(queryMap);

        if(likeFunTopic!=null) {
            isClick = true;
        }
        if(attention!=null) {
            isAttention = true;
        }

        return CommonResult.success(ShareVO.build(funShare,isClick,isAttention)) ;
    }

    @PostMapping("/forward")
    public CommonResult forward(HttpServletRequest request, Long funId) {
        String uid = jwtUtils.getUidFromRequest(request);
        String nickname = jwtUtils.getNameFromRequest(request);
        String avatarUrl = jwtUtils.getAvatarFromRequest(request);
        FunShare funShare = funShareService.findById(funId);
        funShare.setUid(uid);
        funShare.setNickName(nickname);
        funShare.setAvatarUrl(avatarUrl);
        Long id = funShare.getId();
        funShare.setId(null);
        funShare.setLikeNum(null);
        funShare.setForwardNum(null);
        funShare.setCommentNum(null);
        funShare.setPlayNum(null);
        funShare.setSendTime(null);
//        funShare.setContentText("转发 " + funShare.getNickName() + ": " + funShare.getContentText().replace("转发 " + funShare.getNickName() + ": ",""));
        Long insert = funShareService.insert(funShare);
        funShareService.forwardById(id);
        return CommonResult.success(insert);
    }

    @PostMapping(value = "/addShare",produces = "application/json")
    public CommonResult addShare(HttpServletRequest request,FunShare share){
        String authorization = jwtUtils.getTokenFromRequest(request);
        Users users = (Users) redisTemplate.opsForValue().get(authorization);
        if(users==null){
            throw new ForbiddenException(60009);
        }

        share.setUid(users.getUid());
        share.setAvatarUrl(users.getAvatarUrl());
        share.setNickName(users.getNickName());

        funShareService.insert(share);
        return CommonResult.successNoData();
    }

    @PostMapping("/addPlayNum")
    public CommonResult addPlayNum(Long id){
        int i = funShareService.addPlayNum(id);
        return CommonResult.success(i);
    }

    @PostMapping("/getFriendFun")
    public CommonResult getFriendFun(HttpServletRequest request,
                                     @RequestParam(defaultValue = "15") Integer limit,
                                     @RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "999") Integer funClassifyId){
        PageResult<FunShare> pageResult = funShareService.getFunList(request,limit,page,funClassifyId);
        return CommonResult.success(pageResult);
    }

    @PostMapping("/searchFun")
    public CommonResult searchFun(HttpServletRequest request,
                                  @RequestParam(defaultValue = "15") Integer limit,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  String keyWord){
        PageResult<FunShare> pageResult = funShareService.searchFun(request,limit,page,keyWord);
        return CommonResult.success(pageResult);
    }

}
